package com.midea.oms.utils;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @Title: ExcelListener
 * @Description: Excel导入监听类
 *
 *
 * @author: zengsl9
 * @date: 2021年8月1日 下午12:29:31
 * @version V1.0
 *
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ExcelListener<T> extends AnalysisEventListener<T> {

    // 数据列表
    private List<T> dataList = new ArrayList<T>();

    @Override
    public void invoke(T data, AnalysisContext context) {

        if (null != data && !areAllFieldsNull(data)) {
            // 将读取的Excel行数据加到列表中
            dataList.add(data);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    public static boolean areAllFieldsNull(Object obj) {
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (value != null) {
                return false;
            }
        }
        return true;
    }
}
